<template>
    <div class="page-container">
        <!--工具栏-->
        <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
            <el-form :inline="true" :model="filters" :size="size">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" perms="sys:role:view" type="primary"
                               @click="findPage(null)"/>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-plus" :label="$t('action.add')" perms="sys:user:add" type="primary"
                               @click="handleAdd"/>
                </el-form-item>
            </el-form>
        </div>
        <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
            <el-form :inline="true" :size="size">
                <el-form-item>
                    <el-button-group>
                        <el-tooltip content="刷新" placement="top">
                            <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="列显示" placement="top">
                            <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </el-form-item>
            </el-form>
            <!--表格显示列界面-->
            <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns"
                                        @handleFilterColumns="handleFilterColumns">
            </table-column-filter-dialog>
        </div>
        <kt-table :data="pageResult" :showBatchDelete="false" :columns="filterColumns" @findPage="findPage"
                  @handleEdit="handleEdit" @handleDelete="handleDelete">
        </kt-table>

        <!-- 新增修改界面 -->
        <el-dialog :title="!dataForm.id ? '新增' : '修改'" width="40%" :visible.sync="dialogVisible"
                   :close-on-click-modal="false">
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submitForm"
                     label-width="80px" :size="size" style="text-align:left;">
                <el-form-item label="菜单类型" prop="type">
                    <el-radio-group v-model="dataForm.type">
                        <el-radio v-for="(type, index) in menuTypeList" :label="index" :key="index">{{ type }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item :label="menuTypeList[dataForm.type] + '名称'" prop="name">
                    <el-input v-model="dataForm.title" :placeholder="menuTypeList[dataForm.type] + '名称'"></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" prop="parentName">
                    <el-input v-model="dataForm.parentId" placeholder="上级菜单id"></el-input>
                </el-form-item>
                <el-form-item v-if="dataForm.type === 1" label="菜单路由" prop="url">
                    <el-row>
                        <el-col :span="22">
                            <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
                        </el-col>
                        <el-col :span="2" class="icon-list__tips">
                            <el-tooltip placement="top" effect="light" style="padding: 10px;">
                                <div slot="content">
                                    <p>URL格式：</p>
                                    <p>1.常规业务开发的功能URL，如用户管理，Views目录下页面路径为 /Sys/User, 此处填写 /sys/user。</p>
                                    <p>2.嵌套外部网页，如通过菜单打开百度网页，此处填写 http://www.baidu.com，http:// 不可省略。</p>
                                    <p>示例：用户管理：/sys/user 嵌套百度：http://www.baidu.com 嵌套网页：http://127.0.0.1:8000</p></div>
                                <i class="el-icon-warning"></i>
                            </el-tooltip>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="菜单级别" prop="orderNum">
                    <el-input-number v-model="dataForm.level" controls-position="right" :min="1" :max="3"
                                     label="菜单级别"></el-input-number>
                </el-form-item>
                <el-form-item v-if="dataForm.type !== 2" label="菜单图标" prop="icon">
                    <el-row>
                        <el-col :span="22">
                            <el-input v-model="dataForm.icon" :readonly="false" placeholder="菜单图标名称（如：fa fa-home fa-lg）"
                                      class="icon-list__input"></el-input>
                        </el-col>
                        <el-col :span="2" class="icon-list__tips">
                            <fa-icon-tooltip/>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button :size="size" @click="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" @click="submitForm">{{$t('action.comfirm')}}</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
  import PopupTreeInput from '@/components/PopupTreeInput'
  import KtTable from '@/components/Core/KtTable'
  import KtButton from '@/components/Core/KtButton'
  import TableColumnFilterDialog from '@/components/Core/TableColumnFilterDialog'

  export default {
    components: {
      PopupTreeInput,
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    name: 'Menu',
    data () {
      return {
        filters: {
          name: '',
          authorName: ''
        },
        pageRequest: {pageNum: 1, pageSize: 15},
        pageResult: {},
        listMenus: [],
        columns: [],
        filterColumns: [],
        size: 'small',
        dialogVisible: false,
        menuTypeList: ['目录', '菜单'],
        dataForm: {
          id: 0,
          type: 1,
          title: '',
          parentId: '',
          parentName: '',
          status: 1,
          url: '',
          perms: '',
          orderNum: 0,
          level: 1,
          icon: '',
          unique: '',
          iconList: []
        },
      }
    },
    methods: {
      findPage: function (data) {
        if (data != null) {
          this.pageRequest = data.pageRequest
        }
        this.pageRequest.name = this.filters.name
        this.pageRequest.status = 1
        this.$api.menu.findPage(this.pageRequest).then((res) => {
          this.pageResult = res.body
          this.listMenus = res.body
        }).then(data != null ? data.callback : '')
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [
          {prop: 'id', label: 'ID', minWidth: 70},
          {prop: 'title', label: '名称', minWidth: 120},
          {prop: 'url', label: '路由', minWidth: 120},
          {prop: 'createTime', label: '创建时间', minWidth: 120, formatter: this.dateFormat},
          {prop: 'parentId', label: '父ID'},
          {prop: 'unique', label: '唯一码', minWidth: 120},
          {prop: 'type', dialogVisible: false, width: 1}
        ]
        this.filterColumns = this.columns
      },
      handleEdit: function (data) {
        this.dataForm.id = data.row.id
        this.dataForm.type = data.row.type || 1
        this.dataForm.title = data.row.title
        this.dataForm.url = data.row.url
        this.dataForm.level = data.row.level
        this.dataForm.parentId = data.row.parentId
        this.dataForm.parentName = data.row.parentName
        this.dataForm.status = data.row.status
        this.dataForm.icon = data.row.icon
        this.dataForm.orderNum = data.row.orderNum
        this.dataForm.unique = data.row.unique
        this.dialogVisible = true
      },
      handleAdd: function (data) {
        this.dataForm.id = 0;
        this.dataForm.type = 1;
        this.dataForm.title = ''
        this.dataForm.parentId = ''
        this.dataForm.parentName = ''
        this.dataForm.status = 1
        this.dataForm.url = ''
        this.dataForm.perms = ''
        this.dataForm.orderNum = 0
        this.dataForm.level = 1
        this.dataForm.icon = ''
        this.dataForm.unique = ''
        this.dialogVisible = true
      },
      handleDelete: function (data) {
        this.$api.menu.batchDelete({unique: data.params.unique}).then(data != null ? data.callback : '')
      },
      handleTreeSelectChange: function (data) {

      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns
        this.$refs.tableColumnFilterDialog.setDialogVisible(false)
      },
      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true)
      },
      submitForm: function () {
        this.$api.menu.save(this.dataForm).then((res) => {
          if (res.isSuccess == true) {
            this.$message.success('保存成功')
            this.dialogVisible = false
            this.findPage()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    mounted () {
      this.initColumns()
      if (this.listMenus.length == 0) {
        this.$api.menu.findPage({pageSize: 200}).then((res) => {
          this.listMenus = res.body.content
        })
      }
    }
  }
</script>

<style scoped>

</style>
